<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <div class="app-container">
      <el-card shadow="never">
        <el-form
          ref="workArrangeSearchForm"
          :model="workArrangeSearchForm"
          label-width="80px"
        >
          <el-col :span="8">
            <el-form-item
              label="员工账号"
              prop="employeeNumber"
            >
              <el-input
                v-model="workArrangeSearchForm.employeeNumber"
                placeholder="请输入员工账号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="员工姓名"
              prop="name"
            >
              <el-input
                v-model="workArrangeSearchForm.name"
                placeholder="请输入员工姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="工作模式"
              prop="workPatternId"
            >
              <el-select
                v-model="workArrangeSearchForm.workPatternId"
                placeholder="请选择工作模式"
              >
                <el-option
                  v-for="item in selectWorkHistoryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="月份"
              prop="month"
            >
              <el-date-picker
                v-model="workArrangeSearchForm.month"
                type="month"
                value-format="yy-MM"
                placeholder="请选择月份"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="所属机构"
              prop="agencyId"
            >
              <el-select
                v-model="workArrangeSearchForm.agencyId"
                placeholder="请选择所属机构"
              >
                <el-option
                  v-for="item in driverAgentList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item style="margin-left: -68px;">
              <el-button
                type="warning"
                @click="searchWorkArrange"
              >搜索</el-button>
              <el-button @click="resetWorkArrange">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-card>
      <el-card
        style="margin-top: 30px;"
        shadow="never"
      >
        <el-button
          type="warning"
          @click="$router.push('/transit/workArrange-setting')"
        >排班设置</el-button>
        <el-button
          class="is-plain"
          type="warning"
          @click="open"
        >绑定排班</el-button>
        <el-table
          style="margin-top: 30px; width: 100%;"
          :data="workArrangeList"
          :cell-style="{textAlign: 'center'}"
          :header-cell-style="{textAlign: 'center'}"
          stripe
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="50"
          >
          </el-table-column>
          <el-table-column
            fixed
            label="员工账号"
            prop="employeeNumber"
            width="100"
          ></el-table-column>
          <el-table-column
            fixed
            label="员工姓名"
            prop="name"
            width="100"
          ></el-table-column>
          <el-table-column
            fixed
            label="员工电话"
            prop="phone"
            width="150"
          ></el-table-column>
          <el-table-column
            fixed
            label="员工角色"
            width="100"
          >
            <template #default="scope">
              <span v-if="scope.row.userType === 1">员工</span>
              <span v-if="scope.row.userType === 2">快递员</span>
              <span v-if="scope.row.userType === 3">司机</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed
            label="当前工作状态"
            prop="workPatternName"
            width="150"
          ></el-table-column>
          <el-table-column
            v-for="(item, index) in workArrangeList[0].workSchedules"
            :key="index"
            :label="(index + 1).toString()"
            width="100"
          >
            <template #default="scope">
              <span
                v-if="scope.row.workSchedules[index] === true"
                class="working"
              >上</span>
              <span
                v-if="scope.row.workSchedules[index] === false"
                class="relaxing"
              >休</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="workArrangeSearchForm.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="workArrangeSearchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
          style="margin-top: 20px;text-align: center;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </el-card>
      <el-dialog
        title="人工调整"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form
          ref="chooseWorkForm"
          :model="chooseWorkForm"
        >
          <el-form-item>
            <el-radio-group v-model="chooseWorkForm.chooseWork">
              <el-radio label="礼拜制"></el-radio>
              <el-radio label="连续制"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <el-form
          label-width="80px"
          :model="workSckedulingsList"
        >
          <el-form-item label="模式名称">
            <el-select
              v-if="chooseWorkForm.chooseWork === '礼拜制'"
              v-model="workSckedulingsList.workPatternId"
            >
              <el-option
                v-for="item in firstSelectWorkHistoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
            <el-select
              v-if="chooseWorkForm.chooseWork === '连续制'"
              v-model="workSckedulingsList.workPatternId"
            >
              <el-option
                v-for="item in secondSelectWorkHistoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="handleClose">取 消</el-button>
          <el-button
            type="warning"
            @click="putPeopleSet"
          >确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { selectWorkHistoryList, workManage, peopleSet } from '@/api/transit'
import { agencyList } from '@/api/institutions'
export default {
  data () {
    return {
      selectWorkHistoryList: [],
      driverAgentList: [],
      workArrangeSearchForm: {
        agencyId: null,
        employeeNumber: '',
        month: '',
        name: '',
        workPatternId: '',
        page: 1,
        pageSize: 10
      },
      workArrangeList: [],
      counts: 0,
      workSckedulingsList: {
        userIdList: null,
        workPatternId: '',
        workPatternType: ''
      },
      dialogVisible: false,
      firstSelectWorkHistoryList: [],
      secondSelectWorkHistoryList: [],
      chooseWorkForm: {
        chooseWork: '礼拜制'
      }
    }
  },
  mounted () {
    this.getSelectWorkHistoryList()
    this.getDriverAgentList()
    this.getworkManage()
  },
  methods: {
    async getSelectWorkHistoryList() {
      const res = await selectWorkHistoryList()
      const arr = Object.values(res.data)
      const newArr = arr.reduce((acc, val) => {
        return acc.concat(val)
      }, []).map((obj) => {
        return Object.assign({}, obj)
      })
      this.selectWorkHistoryList = newArr
      const arrFirst = newArr.filter(item => item.workPatternType === 1)
      const arrSecond = newArr.filter(item => item.workPatternType === 2)
      this.firstSelectWorkHistoryList = arrFirst
      this.secondSelectWorkHistoryList = arrSecond
    },
    async getDriverAgentList() {
      const res = await agencyList()
      this.driverAgentList = JSON.parse(res.data)
    },
    async getworkManage() {
      const res = await workManage(this.workArrangeSearchForm)
      this.workArrangeList = res.data.items
      this.counts = +res.data.counts
    },
    handleSizeChange(val) {
      this.workArrangeSearchForm.pageSize = val
      this.getworkManage()
    },
    handleCurrentChange(val) {
      this.workArrangeSearchForm.page = val
      this.getworkManage()
    },
    searchWorkArrange() {
      this.getworkManage()
    },
    resetWorkArrange() {
      this.$refs.workArrangeSearchForm.resetFields()
      this.getworkManage()
    },
    handleSelectionChange(val) {
      const newArr = val.map(item => {
        return item.id
      })
      this.workSckedulingsList.userIdList = newArr
    },
    open() {
      if (this.workSckedulingsList.userIdList === null) {
        this.$message.warning('请先选择员工')
      } else {
        this.dialogVisible = true
      }
    },
    async putPeopleSet() {
      if (this.chooseWorkForm.chooseWork === '礼拜制') {
        this.workSckedulingsList.workPatternType = '1'
      } else {
        this.workSckedulingsList.workPatternType = '2'
      }
      const res = await peopleSet(this.workSckedulingsList)
      if (res.code === 200) {
        this.$message.success('成功调整排班')
        this.handleClose()
        this.getworkManage()
      } else {
        this.$message.warning(res.msg)
      }
    },
    handleClose() {
      this.dialogVisible = false
      this.chooseWorkForm = {
        chooseWork: '礼拜制'
      }
      this.workSckedulingsList.workPatternId = ''
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep th.date{
  .cell{
    padding-left: 17px!important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding-bottom: 40px;
  }
}

.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }
  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }
  ::v-deep.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}

.orangeButton {
  border: #e15536 1px solid;
  background-color: #FFF3EF;
  color: #e15536;
}

.working {
  display: inline-block;
  width: 24px;
  height: 24px;
  background: #FFF3EF;
  border: 1px solid rgba(225,85,54,1);
  border-radius: 50%;
  color: #e15536;
  text-align: center;
}

.relaxing {
  display: inline-block;
  width: 24px;
  height: 24px;
  background: #F0F0F0;
  border: 1px solid #DCDFE6;
  border-radius: 50%;
  color: #818693;
  text-align: center;
}
</style>

<style></style>
